

#include <iostream>
using namespace std;

#include "StarcraftRegistryEditor.h"

#include <qcstring.h>

// // DEBUG:
// #include <qfile.h>
// #include <qtextstream.h>


StarcraftRegistryEditor::StarcraftRegistryEditor( QObject* parent, 
						  const char* name )
    : 
    QObject( parent, name ),
    RECENT_GAMES_KEY( "Software\\Battle.net\\Recent Games" ),
    valueKey_("")
{
    
}

bool StarcraftRegistryEditor::writeJoinGame( const QString& gameName )
{
//     QFile debugfile( "debugRegeditJOIN.txt" );
//     debugfile.open( IO_WriteOnly | IO_Append );
//     QTextStream debug( &debugfile );
//     debug << "TOP OF writeJoinGame()" << "\n";
//     debug << "Recent games key == *" << RECENT_GAMES_KEY << "*\n";

    // Trying to get the first value.
#ifdef UNICODE
    CVORegistry registry( HKEY_CURRENT_USER,
			  RECENT_GAMES_KEY.ucs2() );
#else
    CVORegistry registry( HKEY_CURRENT_USER,
			  RECENT_GAMES_KEY.ascii() );
#endif

    CString value;

    hKey_ = registry.getHkey();

    for( bool haveValueKey = registry.GetFirstValue( value );
	 haveValueKey == true;
	 haveValueKey = registry.GetNextValue( value ) )
    {
	// Do some nasty conversions.
	valueKey_ = "";
	unsigned int valueLen = value.GetLength();
	for( unsigned int i = 0; i < valueLen; i++ ) {
	    TCHAR tchar = value.GetAt( i );
	    QChar qchar( tchar );
	    valueKey_ += qchar;
	}
// 	debug << "DEBUG:  the value-key is:  *" << valueKey_ << "*." << "\n";


	// Now doing the actual work.
	DWORD type = REG_SZ;
	DWORD len = gameName.length();
#ifdef UNICODE
	DWORD charSize = 2;
	DWORD nullCharSize = 2;
#endif
// 	debug << "DEBUG:  setting value key:  " << valueKey_ << endl;
// 	debug << "DEBUG:  setting gameName:   " << gameName << endl;

	LONG rc = RegSetValueEx( hKey_,
				 valueKey_.ucs2(),
				 0,
				 type,
#ifdef UNICODE
				 (unsigned char*)(gameName.ucs2()),
				 ((len * charSize) + nullCharSize)
#else
 			         (unsigned char*)(gameName.ascii()),
 			         (len)
#endif
	                         );

	// Since we need to write to every value-data field in this
	// key, we dont' check for success return value.  We assume it
	// wrote okay.
        // 	if( rc == ERROR_SUCCESS )
        // 	{
        // 	}
        // 	else 
        // 	{
        // 	}
    }
    
// 	debugfile.close();
	return true;
}


QString StarcraftRegistryEditor::readJoinGame()
{    
//     QFile debugfile( "debugRegeditJOIN.txt" );
//     debugfile.open( IO_WriteOnly | IO_Append );
//     QTextStream debug( &debugfile );
    

    // Trying to get the first value.
#ifdef UNICODE
    CVORegistry registry( HKEY_CURRENT_USER,
			  RECENT_GAMES_KEY.ucs2() );
#else
    CVORegistry registry( HKEY_CURRENT_USER,
			  RECENT_GAMES_KEY.ascii() );
#endif

    CString value;
    if( !registry.GetFirstValue( value ) ) {
// 	debug << "INFORMATION:  No previous joined games were found." << "\n";
    }
    else 
    {
	// Do some nasty conversions.
	valueKey_ = "";
	unsigned int valueLen = value.GetLength();
	for( unsigned int i = 0; i < valueLen; i++ ) {
	    TCHAR tchar = value.GetAt( i );
	    QChar qchar( tchar );
	    valueKey_ += qchar;
	}
// 	debug << "DEBUG:  the first value has value:  *" 
// 	      << valueKey_ << "*" << "\n";
    }
    hKey_ = registry.getHkey();



    // Now doing the actual work.
    // Obtain the gameName.
    DWORD usedSize = 256;  // 512 should be enough space to hold 
    unsigned char* gameName = new unsigned char[usedSize];
    LONG rc;
    DWORD type = REG_SZ;
    
    rc = RegQueryValueEx( hKey_,
#ifdef UNICODE
			  valueKey_.ucs2(),
#else
			  valueKey_.ascii(),
#endif
			  0,
			  &type,
			  gameName,
			  &usedSize );

//     debug << "Done with work.  " 
// 	  << "      rc == " << rc << ";  ERROR_SUCCESS == " 
// 	  << ERROR_SUCCESS << "\n"
// 	  << "      usedSize == " << usedSize << "\n"
// 	  << "      valueKey_ == " << valueKey_ << "\n";
    
    if( rc == ERROR_SUCCESS ) 
    {
	// Success.
	QByteArray array( usedSize );
	memcpy( (void*)(array.data()), (void*)gameName, usedSize );
	delete [] gameName;
	QString rv;
#ifdef UNICODE
	rv = QString::fromUcs2( (unsigned short*)array.data() );
#else
	rv = QString::fromAscii( (char *)array.data() );
#endif
// 	debug << "rv qstring length == " << rv.length() << "\n";
// 	debugfile.close();
	return rv;
    }
    else 
    {
	// Failure.
// 	debug << "ERROR:  Failed to get the data for the valueKey." << endl;
	delete [] gameName;
	QString rv("");
// 	debug << "rv qstring length == " << rv.length() << ".   (because failed)\n";
// 	debugfile.close();
	return rv;
    }
}

